package org.csu.canteen.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.canteen.entity.*;
import org.csu.canteen.persistence.*;
import org.csu.canteen.service.AccountService;

import org.csu.canteen.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SignonMapper signonMapper;
    @Autowired
    private ProfileMapper profileMapper;
    @Autowired
    private BannerDataMapper bannerDataMapper;
    @Autowired
    private LanguagesMapper languagesMapper;

    //多表联查 account、sigon、profile、bannerdata
    @Override
    public AccountVO getAccount(String username, String password) {
        AccountVO accountVO=new AccountVO();
        Account account=accountMapper.selectById(username);
        Signon signon=signonMapper.selectById(username);
        Profile profile=profileMapper.selectById(username);
        BannerData bannerData=bannerDataMapper.selectById(profile.getFavouriteCategoryId());

        accountVO.setUsername(username);
        accountVO.setPassword(password);
        accountVO.setEmail(account.getEmail());
        accountVO.setFirstName(account.getFirstName());
        accountVO.setLastName(account.getLastName());
        accountVO.setStatus(account.getStatus());
        accountVO.setAddress1(account.getAddress1());
        accountVO.setAddress2(account.getAddress2());
        accountVO.setCity(account.getCity());
        accountVO.setState(account.getState());
        accountVO.setZip(account.getZip());
        accountVO.setCountry(account.getCountry());
        accountVO.setPhone(account.getPhone());
        accountVO.setLanguagePreference(profile.getLangPref());
        accountVO.setListOption(profile.getMyListOpt());
        accountVO.setBannerOption(profile.getBannerOpt());
        accountVO.setBannerName(bannerData.getBannerName());

        return accountVO;
    }

    @Override
    public boolean insertAccount(AccountVO accountVO) {
        Account account=new Account();
        account.setUsername(accountVO.getUsername());
        account.setEmail(accountVO.getEmail());
        account.setFirstName(accountVO.getFirstName());
        account.setLastName(accountVO.getLastName());
        account.setStatus(accountVO.getStatus());
        account.setAddress1(accountVO.getAddress1());
        account.setAddress2(accountVO.getAddress2());
        account.setCity(accountVO.getCity());
        account.setState(accountVO.getState());
        account.setZip(accountVO.getZip());
        account.setCountry(accountVO.getCountry());
        account.setPhone(accountVO.getPhone());
        account.setState(accountVO.getState());
        accountMapper.insert(account);

        Signon signon=new Signon();
        signon.setUsername(accountVO.getUsername());
        signon.setPassword(accountVO.getPassword());
        signonMapper.insert(signon);

        Profile profile=new Profile();
        profile.setUserId(accountVO.getUsername());
        profile.setLangPref(accountVO.getLanguagePreference());
        profile.setFavouriteCategoryId(accountVO.getFavouriteCategory());
        profile.setMyListOpt(accountVO.getListOption());
        profile.setBannerOpt(accountVO.getBannerOption());
        profileMapper.insert(profile);
        return true;
    }

    @Override
    public void updateAccount(AccountVO accountVO) {
        //更新account表
        QueryWrapper<Account> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("userid",accountVO.getUsername());
        accountMapper.update(queryWrapper1);

        //更新profile表
        QueryWrapper<Profile> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("userid",accountVO.getUsername());
        profileMapper.update(queryWrapper2);

        //更新signon表
        if (accountVO.getPassword() != null && accountVO.getPassword().length() > 0) {
            QueryWrapper<Signon> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.eq("username",accountVO.getUsername());
            signonMapper.update(queryWrapper3);
        }
    }

    @Override
    public List<Languages> getLanguages() {
        // 创建一个空的 Languages 列表
        List<Languages> languages = new ArrayList<>();
        // 调用 Mapper 查询所有语言
        List<Languages> languagesFromDB = languagesMapper.getAllLanguages() ;
        // 将查询结果添加到列表中
        languages.addAll(languagesFromDB);
        return languages;
    }

    @Override
    public AccountVO getAccountByUsername(String username) {
        AccountVO accountVO=new AccountVO();
        Account account=accountMapper.selectById(username);
        Signon signon=signonMapper.selectById(username);
        Profile profile=profileMapper.selectById(username);
        BannerData bannerData=bannerDataMapper.selectById(profile.getFavouriteCategoryId());

        accountVO.setUsername(username);
        accountVO.setPassword(signon.getPassword());
        accountVO.setEmail(account.getEmail());
        accountVO.setFirstName(account.getFirstName());
        accountVO.setLastName(account.getLastName());
        accountVO.setStatus(account.getStatus());
        accountVO.setAddress1(account.getAddress1());
        accountVO.setAddress2(account.getAddress2());
        accountVO.setCity(account.getCity());
        accountVO.setState(account.getState());
        accountVO.setZip(account.getZip());
        accountVO.setCountry(account.getCountry());
        accountVO.setPhone(account.getPhone());
        accountVO.setLanguagePreference(profile.getLangPref());
        accountVO.setListOption(profile.getMyListOpt());
        accountVO.setBannerOption(profile.getBannerOpt());
        accountVO.setBannerName(bannerData.getBannerName());

        return accountVO;
    }

    @Override
    public boolean accountExist(String username) {
        if(accountMapper.selectById(username)==null)
        {
            return  false;
        }
        else
            return true;
    }




}
